package com.firemoss.atonement.mapper.resolution
{
	import com.firemoss.atonement.mapper.Mapping;
	import com.firemoss.atonement.metadata.Property;
	
	import flash.utils.Dictionary;
	
	import org.as3commons.reflect.Type;
	
	public class DictionaryValueResolver implements IValueResolver
	{
		public function DictionaryValueResolver()
		{
		}
		
		public static function resolve(source:*, targetProperty : Property, mapping:Mapping, mappingChain:Dictionary):*
		{
			if ( source == null )
			{
				return null
			}
			else if ( mappingChain[ source ] == null )
			{
				var sourceClass : Class = Type.forInstance( source ).clazz
				var result : Dictionary = new sourceClass()
				
				for ( var key : * in source )
				{
					result[ key ] = CollectionValueResolverHelper.createCollectionItem( source, source[key], targetProperty, mapping, mappingChain )
				}
				
				mappingChain[ source ] = result
				
				return result
			}
			else
			{
				return mappingChain[ source ]
			}
		}
		
		public function resolve(source:*, targetProperty : Property, mapping:Mapping, mappingChain:Dictionary):*
		{
			return DictionaryValueResolver.resolve( source, targetProperty, mapping, mappingChain );
		}
	}
}